﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace IncrementProjectApplicationRevision
{
    class Program
    {
        static void Main(string[] args)
        {

            if (args.Length != 1)
                throw new ArgumentException("Just specify the project file, surround with \" if has spaces.");

            var doc = XDocument.Load(args.Single());

            if (doc.Root != null)
            {

                var applicationRevisionElements =
                        from propertyGroups in doc.Root.Descendants()
                        where propertyGroups.Name.LocalName.ToLower() == "PropertyGroup".ToLower() &&
                              propertyGroups.Descendants().Any(y => y.Name.LocalName.ToLower() == "ApplicationRevision".ToLower())
                        select propertyGroups.Descendants().Where(y => y.Name.LocalName.ToLower() == "ApplicationRevision".ToLower());

                var applicationRevisionElement = applicationRevisionElements.Single().Single();

                var applicationRevision = int.Parse(applicationRevisionElement.Value);

                applicationRevision += 1;

                applicationRevisionElement.Value = applicationRevision.ToString();

                doc.Save(args.Single());

            }
            else
            {
                throw new Exception("Wow, root of XML is null.");
            }


        }
    }
}
